using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ScanConfirm.Model;
using ScanConfirm.Service;
using Leeder.Business;
using ScanConfirm.Comm;

namespace ScanConfirm.Business
{
    /// <summary>
    /// 报文历史记录表
    /// </summary>
   public class ReceivesMessageBLL : BaseBLL<ReceivesMessageModel, ReceivesMessageService>
   {
        public ReceivesMessageBLL()
        {
            Service = new ReceivesMessageService();
        }

        /// <summary>
        /// 获取未同步的数据
        /// </summary>
        /// <returns></returns>
        public List<ReceivesMessageModel> GetListNoSync()
        {
            var result = Service.Query(p => p.SyncFlag != 1);
            if (!result.IsSuccess)
            {
                LogObj.Error(result.Dsca);
                return null;
            }
            return result.ActivedObj.Take(500).ToList();
        }

        /// <summary>
        /// 同步数据
        /// </summary>
        /// <param name="jsonStr">JSON字符串</param>
        public bool SyncData(string jsonStr)
        {
            var list = CommonUtil.FromJSON<List<ReceivesMessageModel>>(jsonStr);
            var updateList = list.Where(p => p.SyncFlag == 2).ToList();
            var addList = list.Where(p => p.SyncFlag == 0).ToList();
            list.ForEach(x =>
            {
                x.SyncFlag = 1;
            });
            if (updateList.Count > 0)
            {
                //判断更新的数据是否存在，不存在的添加到插入集合中
                var ids = updateList.Select(x => x.ID).ToArray();//A
                var exitIds = Service.Query(s => ids.Contains(s.ID)).ActivedObj.Select(x => x.ID).ToArray();//B
                var arrAdd = ids.Except(exitIds).ToArray();//A中有B中没有的 （结果：a,b）
                foreach (var id in arrAdd)
                {
                    addList.Add(updateList.Where(a => a.ID == id).FirstOrDefault());
                }

                var result = Service.UpdateList(updateList);
                if (!result.IsSuccess)
                {
                    LogObj.Error(result.Dsca);
                    return false;
                }
                else
                {
                    LogObj.Info("同步成功(更新)，同步数量:" + updateList.Count);
                }
            }
            if (addList.Count > 0)
            {
                var result = Service.AddList(addList, false);
                if (!result.IsSuccess)
                {
                    LogObj.Error(result.Dsca);
                    return false;
                }
                else
                {
                    LogObj.Info("同步成功(插入)，同步数量:" + addList.Count);
                }
            }
            return true;
        }

        /// <summary>
        /// 更新同步状态
        /// </summary>
        /// <param name="jsonStr">JSON字符串</param>
        public void UpdateSyncState(string jsonStr)
        {
            var list = CommonUtil.FromJSON<List<ReceivesMessageModel>>(jsonStr);
            list.ForEach(x =>
            {
                x.SyncFlag = 1;
            });
            var result1 = Service.UpdateList(list);
            if (!result1.IsSuccess)
            {
                LogObj.Error(result1.Dsca);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="jsonStr"></param>
        public void DeleteList(string jsonStr)
        {
            var list = CommonUtil.FromJSON<List<ReceivesMessageModel>>(jsonStr);
            var result = Service.Delete(list);
            if (!result.IsSuccess)
            {
                LogObj.Error(result.Dsca);
            }
        }
    }
}
